﻿from django.shortcuts import render_to_response
from google.appengine.api import memcache
from google.appengine.ext import db
from datetime import datetime, date, time
import re
import logging
import sys
import random

def render(template, payload):
    return render_to_response(template, payload)

def split_tags(s):
    tags = list(set([t.strip() for t in re.split('[,;:\\/\\\\]*', s) if t != ''])) #uniq
    return tags

def formate_time(dt):
    d = dt.split()
    dz = d[0].split('/')
    dy = '20'+ dz[2]
    d_formated = date(int(dy),int(dz[0]),int(dz[1]))
    t = d[1].split(':')
    t_formated = time(int(t[0]),int(t[1]),int(t[2]))
    dp = datetime.combine(d_formated, t_formated)
    return dp

def	gen_duration(ts,te):
    v = abs(te-ts).seconds
    h = v/3600
    m = v%3600/60
    return [h,m]

def gencolor(vcolor,length=2):
    n = int(length)
    rcolor = vcolor[n:]
    return rcolor

def genid(loot_id):
    loot_id=loot_id.split(':')
    return loot_id[0]

def parsetext(data):
    data = data
    data = data.strip()
    data = data.rstrip(';')
    data = data.split(';\n')
    data = data[5:]
    data2 = []
    data3 = []
    dict1 = {}
    for li in data:
        data2.append(li.strip('MiDKPData["dkp"][1]["members"]'))
    for li in data2:
        data3.append(li.split('"] = '))
    for li in data3:
        try:
            dict1[li[0]]=int(li[1])
        except ValueError:
            pass
    return dict1

def retext(data):
    data = data.split('\n')
    dict0 = {}
    for li in data:
        m = re.match(r"(^MiDKPData\[\"dkp\"\]\[1\]\[\"members\"\])(\[\")(\w+|[\x80-\xff]+|[\u3400-\u9FFF]+)(\"\]\s\=\s)(\d+)(\;)",li)
        try:
            dict0[m.group(3)]=m.group(5)
        except AttributeError:
            pass
    return dict0

def gen_quote(num=int):
    try:
        x = random.randint(0,num-1)
    except ValueError:
        x = 0
    return x

